package cn.aws360.boot.extend.open.biz.helper;

import cn.aws360.boot.base.api.exception.ServiceException;
import cn.aws360.boot.extend.open.api.exception.OpenExceptionEnum;
import cn.aws360.boot.extend.open.api.pojo.properties.OpenProperties;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @author junnan
 */
@Slf4j
public class ApiHelper {

    private final static String OPEN_AI_MODEL = "text-davinci-003";

    public static JSONObject getApiResult4Ai(OpenProperties properties, String url, JSONObject paramJson) {
        paramJson.fluentPut("model", OPEN_AI_MODEL).fluentPut("max_tokens", 3000);
        try {
            String result = HttpRequest.post(url)
                    .header(Header.AUTHORIZATION, "Bearer " + properties.getAiApiKey())
                    .body(paramJson.toJSONString())
                    .execute().body();
            if (StrUtil.isNotBlank(result)) {
                return JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            log.error("【getApiResult4Ai】调用异常，url: {}, paramJson: {}", url, paramJson, e);
            throw new ServiceException(OpenExceptionEnum.OPEN_API_GET_EXCEPTION);
        }
        return null;
    }

    public static JSONObject getApiResult4Tl(OpenProperties properties, String url, JSONObject paramJson) {
        paramJson.fluentPut("userInfo",
                new JSONObject()
                        .fluentPut("apiKey", properties.getTlApiKey())
                        .fluentPut("userId", UUID.fastUUID().toString()));
        try {
            String result = HttpRequest.post(url)
                    .body(paramJson.toJSONString())
                    .execute().body();
            if (StrUtil.isNotBlank(result)) {
                return JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            log.error("【getApiResult4Tl】调用异常，url: {}, paramJson: {}", url, paramJson, e);
            throw new ServiceException(OpenExceptionEnum.OPEN_API_GET_EXCEPTION);
        }
        return null;
    }

    public static JSONObject getApiResult4Cu(OpenProperties properties, String url) {
        try {
            String result = HttpRequest.get(url)
                    .header("access-key", properties.getCuApiAccessKey())
                    .header("secret-key", properties.getCuApiSecretKey())
                    .execute().body();
            if (StrUtil.isNotBlank(result)) {
                JSONObject resultJson = JSONObject.parseObject(result);
                if (resultJson.getBoolean("success")) {
                    return resultJson;
                }
            }
        } catch (Exception e) {
            log.error("【getApiResult4Cu】调用异常，url: {}", url, e);
            throw new ServiceException(OpenExceptionEnum.OPEN_API_GET_EXCEPTION);
        }
        return null;
    }

    public static JSONObject getApiResult4Han(String url) {
        try {
            String result = HttpRequest.get(url).execute().body();
            if (StrUtil.isNotBlank(result)) {
                JSONObject resultJson = JSONObject.parseObject(result);
                if (resultJson.getBoolean("success")) {
                    return resultJson;
                }
            }
        } catch (Exception e) {
            log.error("【getApiResult4Han】调用异常，url: {}", url, e);
            throw new ServiceException(OpenExceptionEnum.OPEN_API_GET_EXCEPTION);
        }
        return null;
    }

}
